package com.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.PaymentQiye;
import com.pojo.Qiye;
import com.service.PaymentQiyeService;
import com.service.QiyeService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: T-bao
 * @Date: 2021-4-13 18:09
 * @Description:  企业收费记录action
 */
@RestController
@RequestMapping("/qiyeShoufei")
public class PaymentQiyeAction {

    @Autowired
    private PaymentQiyeService paymentQiyeService;
    @Autowired
    private QiyeService qiyeService;

    /**
     * 修改企业收费记录
     */
    @RequestMapping("/updQiyeShoufei")
    public ResponseBean updQiyeShoufei(@RequestBody PaymentQiye paymentQiye){
        if(paymentQiyeService.updateById(paymentQiye)){
            Qiye qiye = new Qiye();
            qiye.setId(paymentQiye.getQyID());
            qiye.setQyDqtime(paymentQiye.getJsTime());
            qiyeService.updateById(qiye);
            return new ResponseBean(0, 1, "修改成功", "");
        }else{
            return new ResponseBean(1, 1, "修改失败", "");
        }
    }

    /**
     * 新增企业收费记录
     */
    @RequestMapping("/addQiyeShoufei")
    public ResponseBean addQiyeShoufei(@RequestBody PaymentQiye paymentQiye){
        paymentQiye.setCreateTime(new Date());
        paymentQiye.setSfTime(new Date());
        if(paymentQiyeService.save(paymentQiye)){
            Qiye qiye = new Qiye();
            qiye.setId(paymentQiye.getQyID());
            qiye.setQyDqtime(paymentQiye.getJsTime());
            qiyeService.updateById(qiye);
            return new ResponseBean(0, 1, "缴费成功", "新增企业收费成功");
        }else{
            return new ResponseBean(1, 1, "缴费失败", "新增企业收费失败");
        }
    }


    /**
     * 查询企业收费记录
     * @param qiye    企业ID
     * @param page    页数
     * @param limit   每页条数
     * @return
     */
    @RequestMapping("/qiyeShoufeijilu")
    public ResponseBean selectShoufei(String qiye, int page, int limit, @RequestParam(required = false) String start_time, @RequestParam(required = false) String end_time){

        if(StringUtil.isNull(start_time)){
            start_time=null;
        }
        if(StringUtil.isNull(end_time)){
            end_time=null;
        }
        if(StringUtil.isNull(qiye)){
            qiye=null;
        }
        IPage iPage = paymentQiyeService.selectQiyeID(qiye,page,limit,start_time,end_time);
        return new ResponseBean(0 , iPage.getTotal(), "成功",iPage.getRecords() );
    }
}
